var ck = {
	util: {
		checkName: function(dsName, type, pageId) { // 对参数名进行检测
			var isRepeat;
			$.ajax({
				url: 'editor/util/checkNameIsRepeat',
				data: {
					name: dsName,
					type: type,
					pageId: pageId
				},
				type: 'Post',
				success: function(state) {
					isRepeat = state;
				},
				async: false
			});

			if (isRepeat) {
				alert(ck.constant.checkNameIsRepeatMsg);
				return true;
			}
		},
		builTableData: function(id, height, dom) {
			var table = $('#' + id, dom).DataTable({
				"scrollY": height + "px",
				"paging": false,
				"bSort": false,
				"scrollX": true,
				'searching': false,
				'bInfo': false,
				"sDom": 'rt'
			});

			// new $.fn.dataTable.FixedColumns(table, {
			// 	fixedColumns: {
			// 		leftColumns: 1
			// 	}
			// });
		},
		sendAjax: function(sendData, success, error) {
			var url = sendData.url;
			var data = sendData.data;
			var timeout = 0;
			var context = sendData.context;
			var defError;
			if (error) {
				defError = error;
			} else {
				defError = function(jqXHR, textStatus, errorThrown) {
					ck.util.showMsg(2, errorThrown);
				}
			}
			var def = {
				url: url,
				timeout: timeout,
				data: data,
				type: 'post'
			};
			if (context) {
				$.extend(def, {
					context: context
				});
			}

			$.ajax(def).done(success).fail(defError).always(function() {
				var codeObjsMap = $_doc.data('codeObjsMap');
				if (codeObjsMap) {
					var len = Object.keys(codeObjsMap).length;
					if (len == 0) {
						var msgBox = $('#msgBoxDiv');
						$_doc.removeData('codeObjsMap');
						msgBox.fadeOut(600);
					}
				}
			});
		},
		showMsg: function(state, txt) {
			// state  0 loading,1 save,2 error
			var time = new Date().format('hh:mm');
			var msgBox = $('#msgBoxDiv');
			if (!msgBox.is(':hidden')) {
				return;
			}
			var msg = msgBox.find('.msg');
			msg.find('.txt').text(time + ' ' + txt);
			$('#loadingGif').hide();

			if (state == 0) { // loading
				msg.css('background', '#89B5E9');
				$('#loadingGif').show();
			} else if (state == 1) { // save
				msg.css('background', '#89B5E9');
			} else if (state == 2) { // error
				msg.css('background', '#E98989');
				// msgBox.find('.closeMsg').show();
			} else if (state == 3) {
				msg.css('background', '#E98989');
			}

			msgBox.fadeIn(500, function() {
				setTimeout(
					function() {
						if (state != 0) {
							msgBox.fadeOut(600);
						}
					}, 1000);
			});
		},
		randomString: function(length, chars) {
			var mask = '';
			if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz';
			if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			if (chars.indexOf('#') > -1) mask += '0123456789';
			if (chars.indexOf('!') > -1) mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
			var result = '';
			for (var i = length; i > 0; --i) result += mask[Math.round(Math.random() * (mask.length - 1))];
			return result;
		},
		saveArgsToServer: function(ftArgs) { // 将参数缓存到后台
			$.ajax({
				url: 'editor/useArgList/saveArgsCache',
				data: {
					argJsons: JSON.stringify(ftArgs)
				},
				method:'post',
				async: false
			})
		},
		getSaveArgs: function() {
			//获得参数
			$.get('editor/useArgList/getParamByPageId?pageId=' + pageId, function(data) {
				var ftArgs = [];
				var refDateScript = [];
				dateTypeArgs = [];
				
				for (var i = 0; i < data.length; i++) {
					var line = data[i];
					var argType = line.argType,
						argName = line.argName,
						argValue = line.argValue,
						showValue = line.showValue,
						isScript = line.isScript,
						isShow = line.isShow,
						id = line.id;

					if (argType == 'DAY' || argType == 'DATETIME') {
						if (isScript) {
							scriptDataTypeArgs[argName] = line;
						} else {
							dateTypeArgs[argName] = line;
						}
					} else {
						ftArgs.push(line);
					}
				}
				// 设置一个跳出值
				var breakSize = 0;
				while (true) {
					for (var key in scriptDataTypeArgs) {
						var line = scriptDataTypeArgs[key];
						var scriptCode = line.argValue;
						var obj = eval('sys.date.' + scriptCode);
						// 非空，并且正常获得值
						if (obj && obj.val != -1) {
							line.showValue = obj.val;
							if (obj.refDateScript) {
								refDateScript.push(line);
							}
							dateTypeArgs[key] = line;
							delete scriptDataTypeArgs[key];
						}
					}
					breakSize++;
					if (breakSize == 10) {
						for (var key in scriptDataTypeArgs) {
							scriptDataTypeArgs[key].showValue = '配置错误';
							ftArgs.push(scriptDataTypeArgs[key]);
						}
						break;
					}
				}
				for (var key in dateTypeArgs) {
					ftArgs.push(dateTypeArgs[key]);
				}
				dateTypeArgs = [];
				scriptDataTypeArgs = [];
				// 排序
				function sortArgs(a, b) {
					var firstOrder = a.orderId;
					var secondOrder = b.orderId;
					return firstOrder - secondOrder;
				}

				$_doc.data('ftArgs', ftArgs.sort(sortArgs));
				$_doc.data('refDateScript', refDateScript);
				ck.util.saveArgsToServer(ftArgs);
			});
		}
	},
	constant: {
		checkNameIsRepeatMsg: '变量名与其他变量名重复，请重新指定。'
	}
};